package org.example.ui;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.util.List;

public class UserAgentSelectBox<String> extends JComboBox {
    public String userAgent;

    public UserAgentSelectBox(JFrame jf, String ... item) {
        if (item == null || item.length == 0) {
            URL url = UserAgentSelectBox.class.getClassLoader().getResource("userAgent.txt");
            File file = new File(url.getFile());
            try {
                List<java.lang.String> items = Files.readAllLines(file.toPath());
                item = items.toArray(item);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        this.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                selected();
            }
        });
        userAgent = item[0];
        for (String s : item) {
            this.addItem(s);
        }
        jf.add (this);
    }

    public void selected() {
        String item = (String) this.getSelectedItem();
        userAgent = item;
    }
}
